package org.gdstash.item;

import org.gdstash.db.DBItem;
import org.gdstash.db.ItemClass;
import org.gdstash.file.GDFileSize;
import org.gdstash.file.GDReader;
import org.gdstash.file.GDWriter;
import org.gdstash.util.GDLog;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class GDTransmuteType implements GDFileSize {
    private int itemType;
    private List<String> items;

    public GDTransmuteType() {
        this.items = new LinkedList<String>();
    }

    public GDTransmuteType(final int itemType) {
        this();
        this.itemType = itemType;
    }

    public int getTransmuteType() {
        return this.itemType;
    }

    public void insertItem(final DBItem item) {
        if (item == null) {
            return;
        }
        final int type = ItemClass.getTransmuteType(item.getItemClass());
        final String meshID = item.getMeshID();
        final String baseID = item.getBaseTextureID();
        final String bumpID = item.getBumpTextureID();
        final String glowID = item.getGlowTextureID();
        final String shaderID = item.getShaderID();
        final String itemID = item.getItemID();
        final int level = item.getItemLevel();
        if (type != this.itemType) {
            return;
        }
        if (meshID == null) {
            return;
        }
        if (meshID.equals("items/gearaccessories/medals/medal_visible.msh")) {
            return;
        }
        if (itemID == null) {
            return;
        }
        if (item.isEnemyOnly()) {
            return;
        }
        boolean found = false;
        final Iterator<String> iter = this.items.iterator();
        while (iter.hasNext()) {
            final String s = iter.next();
            final DBItem dbi = DBItem.get(s);
            if (dbi != null) {
                if (itemID.equals(dbi.getItemID())) {
                    found = true;
                    break;
                }
                if (!meshID.equals(dbi.getMeshID())) {
                    continue;
                }
                final String base2 = dbi.getBaseTextureID();
                final String bump2 = dbi.getBumpTextureID();
                final String glow2 = dbi.getGlowTextureID();
                final String shader2 = dbi.getShaderID();
                boolean baseFound = false;
                boolean bumpFound = false;
                boolean glowFound = false;
                boolean shaderFound = false;
                if (baseID == null) {
                    if (base2 == null) {
                        baseFound = true;
                    }
                } else if (baseID.equals(base2)) {
                    baseFound = true;
                }
                if (bumpID == null) {
                    if (bump2 == null) {
                        bumpFound = true;
                    }
                } else if (bumpID.equals(bump2)) {
                    bumpFound = true;
                }
                if (glowID == null) {
                    if (glow2 == null) {
                        glowFound = true;
                    }
                } else if (glowID.equals(glow2)) {
                    glowFound = true;
                }
                if (shaderID == null) {
                    if (shader2 == null) {
                        shaderFound = true;
                    }
                } else if (shaderID.equals(shader2)) {
                    shaderFound = true;
                }
                if (!baseFound || !bumpFound || !glowFound || !shaderFound) {
                    continue;
                }
                if (level >= dbi.getItemLevel()) {
                    found = true;
                    break;
                }
                iter.remove();
            }
        }
        if (!found) {
            this.items.add(itemID);
        }
    }

    public void removeItem(final DBItem item) {
        if (item == null) {
            return;
        }
        final int type = ItemClass.getTransmuteType(item.getItemClass());
        final String meshID = item.getMeshID();
        final String baseID = item.getBaseTextureID();
        final String bumpID = item.getBumpTextureID();
        final String itemID = item.getItemID();
        final int level = item.getItemLevel();
        if (type != this.itemType) {
            return;
        }
        final Iterator<String> iter = this.items.iterator();
        while (iter.hasNext()) {
            final String s = iter.next();
            if (itemID.equals(s)) {
                iter.remove();
                break;
            }
        }
    }

    public void read(final GDLog log) throws IOException {
        final GDReader.Block block = new GDReader.Block();
        final int val = GDReader.readBlockStart(block);
        this.itemType = GDReader.readEncInt(true);
        final int numItems = GDReader.readEncInt(true);
        this.items.clear();
        for (int i = 0; i < numItems; ++i) {
            final String itemName = GDReader.readEncString();
            this.items.add(itemName);
        }
        GDReader.readBlockEnd(block);
    }

    @Override
    public int getByteSize() {
        int size = 0;
        size += 4;
        size += 4;
        size += 4;
        size += 4;
        for (final String s : this.items) {
            size += 4;
            size += s.length();
        }
        size += 4;
        return size;
    }

    public void write() throws IOException {
        final GDReader.Block block = new GDReader.Block();
        GDWriter.writeBlockStart(block, 0);
        GDWriter.writeInt(this.itemType);
        GDWriter.writeInt(this.items.size());
        for (final String s : this.items) {
            GDWriter.writeString(s);
        }
        GDWriter.writeBlockEnd(block);
    }
}
